define(["easy-admin", "jquery"], function (ea, $) {
    // 登录过期的时候，跳出ifram框架
    if (top.location != self.location) top.location = self.location;
    // 进行登录操作
    $('#login-btn').on('click', function(data){
        handelLogin()
    });
    function handelLogin(){
        if ($("#keep_login").is(":checked")== true) {
        //选中触发事件
        $('#keep_login_val').val(1)
        } else {
        //取消选中触发事件
        $('#keep_login_val').val(0)
        }

        $.ajax({
        //几个参数需要注意一下
            type: "POST",//方法类型
            dataType: "json",//预期服务器返回的数据类型
            url: "/" + $('#login-btn').attr('model') + "/login/index" ,//url
            data: $('#form').serialize(),
            success: function (result) {
                if (result.code == 1) {
                    ea.msg.success('登录成功', function () {
                        window.location = '/' + $('#login-btn').attr('model') + '/index.html';
                    });
                    return false;
                }else{
                    ea.msg.error(result.msg)
                    $('#captchaPic').click()
                }
                
            },
            error : function() {
                alert("异常！");
            }
        });
    }
    var Controller = {
        index: function () {

            if (top.location !== self.location) {
                top.location = self.location;
            }

            $('.bind-password').on('click', function () {
                if ($(this).hasClass('icon-5')) {
                    $(this).removeClass('icon-5');
                    $("input[name='password']").attr('type', 'password');
                } else {
                    $(this).addClass('icon-5');
                    $("input[name='password']").attr('type', 'text');
                }
            });

            $('.icon-nocheck').on('click', function () {
                if ($(this).hasClass('icon-check')) {
                    $(this).removeClass('icon-check');
                } else {
                    $(this).addClass('icon-check');
                }
            });
            
            $('.login-tip').on('click', function () {
                $('.icon-nocheck').click();
            });

            ea.listen(function (data) {
                data['keep_login'] = $('.icon-nocheck').hasClass('icon-check') ? 1 : 0;
                return data;
            }, function (res) {
                ea.msg.success(res.msg, function () {
                    window.location = ea.url('index');
                })
            }, function (res) {
                ea.msg.error(res.msg, function () {
                    $('#refreshCaptcha').trigger("click");
                });
            });

        },
    };
    return Controller;
});
